<template>
  <div class>
    <nly-container :fluid="true">
      <nly-breadcrumb>
        <nly-breadcrumb-item
            icon="nlyfont nly-icon-home-fill"
            to="/"
            text="首页"
        />
        <nly-breadcrumb-item
            icon="nlyfont nly-icon-outlet"
            to="/food"
            active
            text="提现申请"
        />
      </nly-breadcrumb>
      <Modal
          :title= "'审核提现'"
          :styles="{ width: '50%' }"
          v-model="checkWithDrawModal"
          class-name="vertical-center-modal"
          :footer-hide="true"
          @on-cancel="cancel"
      >
        <div>
          <Form ref="dataText" :model="dataText" :label-width="130">
            <FormItem label="审核操作" prop="state">
              <Select
                  v-model="dataText.state"
                  style="width: 200px"
                  placeholder="请选择审核操作"
              >
                <Option :value="1">审核成功</Option>
                <Option :value="2">审核失败</Option>
              </Select>
            </FormItem>
            <FormItem label="审核原因" prop="remark">
              <Input v-model="dataText.remark" placeholder="请输入审核意见" />
            </FormItem>
            <FormItem>
              <Button type="primary" @click="checkDraw('dataText')">提交</Button>
              <Button @click="ModalClose" style="margin-left: 8px">取消</Button>
            </FormItem>
          </Form>
        </div>
      </Modal>

      <section class="content">
        <row>
          <RadioGroup
              v-model="data.state"
              @on-change="changeType"
              style="float: left; margin-left: 20px"
          >
            <Radio :label="0" border>待审核</Radio>
            <Radio :label="1" border>审核成功</Radio>
            <Radio :label="2" border>审核失败</Radio>
          </RadioGroup>
        </row>
        <br />
        <Table
            border
            :columns="header"
            :data="accountList"
            @on-selection-change="enterall"
            ref="selection"
        >
          <template slot-scope="{ row }" slot="img">
            <img
                v-if="arrSplit(row.img)"
                :src="arrSplit(row.img)"
                width="60"
                height="60"
                style="padding: 3px"
                class="image"
            />
          </template>
          <template slot-scope="{ row }" slot="resolveStatus">{{
              row.resolveStatus === 0? "待审核" : row.resolveStatus === 1 ? "审核成功" : "审核失败"
            }}</template>
          <template slot-scope="{ row }" slot="resolveUser">{{
              row.resolveUser == ''? "无" : row.resolveUser
            }}</template>
          <template slot-scope="{ row }" slot="remark">{{
              row.remark == ''? "无" : row.remark
            }}</template>
          <template slot-scope="{ row }" slot="withDrawMoney">{{
              row.withDrawMoney == ''? "无" : row.withDrawMoney
            }}</template>
          <template slot-scope="{ row }" slot="action">
            <Button v-if="row.resolveStatus === 0"
                type="success"
                size="small"
                @click="check(row.id)"
            >审核</Button>
            <div v-if="row.resolveStatus !== 0"
                    type="success"
                    size="small"
            >已审核</div>
          </template>
        </Table>
        <br />
        <div class="page">
          <Page
              :total="total"
              show-elevator
              show-total
              :page-size="data.pageSize"
              :current="data.page"
              @on-change="changePage"
          />
        </div>
      </section>
    </nly-container>
  </div>
</template>
<script>
import apis from "@/api/shop1";
export default {
  components: {},
  data() {
    return {
      modalAddText: false,
      modalAddDiscountCode: false,
      update: false,
      searchText: "",
      total: 0,
      checkWithDrawModal: false,
      data: {
        page: 1,
        userId: '',
        pageSize: 10,
        state: 0,
      },
      header: [
        {
          title: "申请人",
          key: "accountName",
          align: "center",
        },
        {
          title: "微信号",
          key: "weChat",
          align: "center"
        },
        {
          title: "提现金额(元)",
          slot: "withDrawMoney",
          align: "center",
        },

        {
          title: "处理人",
          slot: "resolveUser",
          align: "center",
        },
        {
          title: "处理状态",
          slot: "resolveStatus",
          align: "center",
        },
        {
          title: "审核原因",
          slot: "remark",
          align: "center",
        },
        {
          title: "创建时间",
          key: "createTime",
          align: "center",
        },
        {
          title: "操作",
          fixed: "right",
          slot: "action",
          width: 120,
          align: "center",
        },
      ],
      accountList: [],
      count_scale: [],
      pageNow: 1,
      selectclass: [],
      dataText: {
        id: "",
        remark:"",
        state: "",
      },
      codeText: {
        discountId: "",
        count: "",
      },

      key: "",
      id: "",
    };
  },
  mounted() {
    let that = this;
    if (typeof that.$route.query.id != "undefined") {
      that.data.userId = that.$route.query.id
    }
    that.getcupponlist();
  },
  methods: {

    check(id) {
      this.checkWithDrawModal = true
      this.dataText.id = id
    },
    checkCancel(id) {
      apis.checkCancel(id)
      .then((response) => {
        if (response.data.code === 0) {
          this.getcupponlist()
          this.$Message.success("撤销成功")
        }else {
          this.getcupponlist()
          this.$Message.error("撤销失败!")
        }
      })
    },
    checkDraw() {
      apis.checkWithDraw(this.dataText)
          .then((response) => {
            if (response.data.code === 0) {
              this.getcupponlist()
              this.$Message.success("审核提现成功!")
            }else {
              this.$Message.error("审核失败!")
            }
            this.checkWithDrawModal = false
            this.getcupponlist()
          }).catch((error) => {
        console.log(error)
        this.modalAddDiscountCode = false
        this.checkWithDrawModal = false
        this.$Message.error("审核失败")
      })
    },
    listDiscountCode(id) {
      this.$router.push({
        path: "/shop/cupponCode",
        query: {
          id: id,
        },
      });
    },
    addCodeModal(id) {
      this.codeText.discountId = id
      this.modalAddDiscountCode = true
    },
    addCodeText() {
      apis.saveDiscountCode(this.codeText)
          .then((response) => {
            if (response.data.code === 0) {
              this.$Message.success("添加优惠券码成功!")
              this.modalAddDiscountCode = false
            }else {
              this.$Message.error("添加失败!")
            }
          }).catch((error) => {
        console.log(error)
        this.modalAddDiscountCode = false
        this.$Message.error("添加失败")
      })
    },
    changeType(val) {
      this.data.page = 1;
      this.data.state = val;
      console.log(this.data2);
      this.getcupponlist();
    },
    addaccount(id) {
      this.$router.push({
        path: "/addaccoundq",
        query: {
          id: id,
        },
      });
    },
    addShow() {
      this.modalAddText = true;
    },
    enterall(e) {
      this.enteralldata = e;
      console.log(e);
    },
    remove(id) {
      let dc_id = "";
      if (id) {
        dc_id = id;
      } else {
        this.enteralldata.map((itme) => {
          if (dc_id == "") {
            dc_id = itme.dc_id;
          } else {
            dc_id += "," + itme.dc_id;
          }
        });
      }
      apis
          .deletecuppon({ id: dc_id })
          .then((response) => {
            console.log(response);
            if (response.data.code == 0) {
              this.$Message.success("删除成功");
              this.getcupponlist();
            } else {
              this.$Message.error("删除失败!");
            }
          })
          .catch((error) => {
            console.log(error);
            this.loading = false;
            this.$Message.error("删除失败!");
          });
    },
    getcupponlist() {
      let that = this;
      apis.getWithDrawList(this.data).then((response) => {
        let resdata = response.data;
        if (resdata.code === 0) {
          console.log(response.data);
          that.accountList = response.data.data.records;
          that.total = resdata.data.total;
        }else if (resdata.code === 201) {
          that.accountList = null;
          that.total = 0;
        }
      });
    },
    changePage(page) {
      this.data.page = page;
      this.getcupponlist();
    },
    search_account(val) {
      this.data.page = 1;
      let data = this.data;
      data.name = val;
      this.getcupponlist();
    },
    ModalClose() {
      this.modalAddText = false;
      this.update = false;
      this.modalAddDiscountCode = false;
      this.checkWithDrawModal = false;
    },
    arrSplit(str) {
      if ((str || "").split(",")[0]) {
        return str.split(",")[0];
      } else {
        return "";
      }
    },
    changeText(id, data) {
      console.log("打开模态框");
      this.update = true;
      this.dataText.name = data.name;
      this.dataText.meet = data.meet;
      this.dataText.subtract = data.subtract;
      this.dataText.outTime = data.outTime;
      this.dataText.discount = data.discount;
      this.dataText.price = data.price;
      this.dataText.type = data.type;
      this.dataText.state = data.state;
      this.dataText.maximum = data.maximum;
      this.modalAddText = true;
      this.id = id;
      console.log(this.id);
    },
    AddText(name) {
      console.log(name);
      console.log(this.update);
      var that = this;
      this.$refs[name].validate((valid) => {
        if (valid) {
          if (this.update) {
            this.dataText.id = this.id;
            apis
                .updatecuppon({
                  id: this.dataText.id,
                  name: this.dataText.name,
                  outTime: this.dataText.outTime,
                  meet: this.dataText.meet,
                  discount: this.dataText.discount,
                  price: this.dataText.price,
                  type: this.dataText.type,
                  state: this.dataText.state,
                  maximum: this.dataText.maximum,
                })
                .then((response) => {
                  if (response.data.code == 0) {
                    this.$Message.success("修改成功");
                    that.getcupponlist();
                    this.modalAddText = false;
                    this.update = false;
                    this.dataText.subtract = "";
                    this.$refs.dataText.resetFields();
                  } else {
                    this.$Message.error("修改失败");
                  }
                });
          } else {
            apis
                .addcuppon({
                  name: this.dataText.name,
                  outTime: this.dataText.outTime,
                  meet: this.dataText.meet,
                  subtract: this.dataText.subtract,
                  discount: this.dataText.discount,
                  price: this.dataText.price,
                  type: this.dataText.type,
                  state: this.dataText.state,
                  maximum: this.dataText.maximum,
                })
                .then((response) => {
                  this.loading = false;
                  let resdata = response.data;
                  if (resdata.code == 0) {
                    this.$refs.dataText.resetFields();
                    this.modalAddText = false;
                    this.update = false;
                    this.dataText.subtract = "";
                    this.$Message.success("添加成功");
                    that.getcupponlist();
                  } else if (resdata.code == 201) {
                    this.$Message.success("账号重复添加");
                  }
                });
          }
        }
      });
    },
    cancel() {
      this.$refs.dataText.resetFields();
      this.dataText.name = "";
      this.dataText.maximum = "";
      this.dataText.meet = "";
      this.dataText.subtract = "";
      this.dataText.discount = "";
      this.dataText.outTime = "";
      this.dataText.price = "";
      this.dataText.state = "";
      this.dataText.type = "";
      this.modalAddText = false;
      this.update = false;
    },
  },
};
</script>

<style scoped>
.padding20 {
  margin: 20px;
}

.avatar {
  width: 50px;
  height: 50px;
  border-radius: 90px;
  margin: 5px;
}
.Page {
  display: flex;
  justify-content: center;
  text-align: center;
  padding-bottom: 10px;
}
.ivu-modal-body {
  padding: 16px;
  font-size: 14px;
  line-height: 1.5;
  text-align: center;
}
.page {
  text-align: center;
}
</style>